package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Activity;
import com.gaohe.pojo.Category;
import com.gaohe.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.dataflow.qual.Pure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
@Slf4j
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    //活动分页查询
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) String name){
        Page<Activity> activityPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Activity> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Activity::getActivityName,name);
        qw.orderByDesc(Activity::getEndTime);
        activityService.page(activityPage,qw);
        return R.success(activityPage);
    }
    //活动修改
    //回显
    @GetMapping("/{id}")
    public R findbyid(@PathVariable Long id){
        return R.success(activityService.getById(id));
    }
    //修改
    @PutMapping
    public R update(@RequestBody Activity activity)
    {
        boolean b = activityService.updateById(activity);
        return b?R.success("修改成功"):R.error("修改失败");
    }
    @DeleteMapping
    public R delete(@RequestParam Long id){
        boolean b = activityService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }
    @PostMapping
    public R save(@RequestBody Activity activity)
    {
        return R.success(activityService.save(activity));

    }
    @GetMapping("/getActivityList")
    public R getActivityList(){
        List<Activity> list = activityService.list();
        return R.success(list);
    }


}
